/**
 * Created by jovi on 3/11/16.
 */
$(document).ready(function () {
  shareConfig();
});

function shareConfig() {
  var share = {
    imgUrl: 'http://point.xiqing.info/ws/static/common/images/share.jpg',
    link: 'http://www.changanfordwechat.com/fordwechat/control?state=4000',
    title: "新年新享，1元众筹",
    desc: "长安福特微商城新年新众筹，好礼等你来"
  };
  $.getJSON('/ws-api/api/public/wechat/info?state=4&url=' + encodeURIComponent(location.href.split('#')[0]), function (response) {
    wxshare(response);
  });

  function wxshare(response) {
    if (response.code != "OK") {
      return;
    }


    wx.config({
      debug: false,
      appId: response.body.appId,
      timestamp: response.body.timestamp,
      nonceStr: response.body.nonceStr,
      signature: response.body.signature,
      jsApiList: [
        'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'chooseImage',
        'previewImage',
        'uploadImage',
        'downloadImage',
      ]
    })

    wx.ready(function () {
      wxcheck();

      function wxcheck() {
        wx.checkJsApi({
          jsApiList: [
            'checkJsApi',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'onMenuShareQQ',
            'onMenuShareWeibo'
          ],
          success: function (res) {
            //alert(JSON.stringify(res));

          }
        });
      }

      wx.onMenuShareTimeline({
        title: share.desc, // 分享标题
        link: share.link, // 分享链接
        imgUrl: share.imgUrl,
        desc: share.desc,
        success: function () {


        },
        cancel: function () {
          // 用户取消分享后执行的回调函数
        }
      });


      wx.onMenuShareAppMessage({
        title: share.title, // 分享标题
        link: share.link, // 分享链接
        imgUrl: share.imgUrl,
        desc: share.desc,
        trigger: function (res) {
          //	alert('用户点击分享到朋友圈');
        },
        success: function (res) {


        },
        cancel: function (res) {
          //	alert('已取消');
        },
        fail: function (res) {
          //	alert(JSON.stringify(res));
        }
      });

      wx.onMenuShareQQ({
        title: share.title, // 分享标题
        desc: share.desc, // 分享描述
        link: share.link, // 分享链接
        imgUrl: share.imgUrl, // 分享图标
        success: function () {
          // 用户确认分享后执行的回调函数
        },
        cancel: function () {
          // 用户取消分享后执行的回调函数
        }
      });
      wx.onMenuShareWeibo({
        title: share.title, // 分享标题
        desc: share.desc, // 分享描述
        link: share.link, // 分享链接
        imgUrl: share.imgUrl, // 分享图标
        success: function () {
          // 用户确认分享后执行的回调函数
        },
        cancel: function () {
          // 用户取消分享后执行的回调函数
        }
      });

    });

  }
}
